﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Accounting;
using School.Common;

namespace School.Pages.Accounting
{
    public partial class LatePayments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(107) == true)
                {

                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        ListBox1.DataSource = Expense.GetAllExpenses();
                        ListBox1.DataValueField = "ID";
                        ListBox1.DataTextField = "Name";
                        ListBox1.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
                        lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataSource = Structure.GetClassesByGrade(long.Parse(LstGrades.SelectedValue));
            lstClasses.DataValueField = "Id";
            lstClasses.DataTextField = "Name";
            lstClasses.DataBind();
            lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });

            lstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            lstStudents.DataValueField = "Id";
            lstStudents.DataTextField = "Name";
            lstStudents.DataBind();
            lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void lstClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstStudents.DataSource = Students.GetStudentsByClass(int.Parse(lstClasses.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            lstStudents.DataValueField = "Id";
            lstStudents.DataTextField = "Name";
            lstStudents.DataBind();
            lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            long campus = long.Parse(LstCampuses.SelectedValue);
            long certificate = long.Parse(LstCertificates.SelectedValue);
            long stage = long.Parse(LstStages.SelectedValue);
            long grade = long.Parse(LstGrades.SelectedValue);
            int xclass = int.Parse(lstClasses.SelectedValue);
            long student = long.Parse(lstStudents.SelectedValue);

            /*if (student == 0)
                return;*/

            List<long> expenses = new List<long>();

            foreach (ListItem i in ListBox1.Items)
            {
                if (i.Selected)
                    expenses.Add(long.Parse(i.Value));
            }

            BalancesPanel.Visible = false;
            VouchersPanel.Visible = false;
            PaymentsPanel.Visible = false;

            BalanceGrid.DataSource = LatePayment.GetBalance(campus, certificate, stage, grade, xclass, student)
                .Select(x => new { Student_Name = x.TE_Student.Ar_FirstName + " " + x.TE_Student.Ar_SecondName + " " + x.TE_Student.Ar_ThirdNmae + " " + x.TE_Student.Ar_FourthName, Student_Number = x.TE_Student.RegistrationNumber, x.Value, Student_Id = x.TE_Student.Id, Type = 1 });
            BalanceGrid.DataBind();

            if (BalanceGrid.Rows.Count > 0)
                BalancesPanel.Visible = true;

            IncompleteVouchersGrid.DataSource = LatePayment.GetVouchers(campus, certificate, stage, grade, xclass, student, expenses);
            IncompleteVouchersGrid.DataBind();

            if (IncompleteVouchersGrid.Rows.Count > 0)
                VouchersPanel.Visible = true;

            PaymentsGrid.DataSource = LatePayment.GetPayments(campus, certificate, stage, grade, xclass, student, expenses);
            PaymentsGrid.DataBind();

            if (PaymentsGrid.Rows.Count > 0)
                PaymentsPanel.Visible = true;
        }

        protected void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}